// import Vue from 'vue'
import axios from 'axios'
import router from "@/router";

class MatchConfirmChain {
  constructor() {
    this.url = '/quotation/template/match';
    this.router = "quoteDetail";
  }
  async confirm ({ subjectId }, $vue) {
    const { code, message, data } = await axios.post(this.url, { subjectId });
    if (0 !== code) {
      console.log("匹配合约异常", code, message)
      return Promise.reject(new Error("匹配合约异常"))
    }

    if (!data.match) {
      // TOOD: 跳转到报价页面
      return Promise.reject(new Error("没有匹配到报价合约"));
    }
  }
}


class RepeatConfirmChain {
  confirm ({ subjectId }, $vue) {
    return $vue.$confirm("不能重复报价，请刷新页面?", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      $vue.search();
      return Promise.reject(new Error("不能重复报价"))
    });
  }
}

class AuditConfirmChain {
  constructor() {
    this.router = "quoteDetail";
  }
  confirm ({ subjectId, waybillId, quotationId, surcharge }, $vue) {
    return $vue.$confirm(`已向您发起了报价，是否去审核?`, "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      // TODO: 跳转到审核页面
      router.push({
        name: this.router,
        query: {
          quotationId: quotationId,
          subjectId: subjectId,
          waybillId: waybillId,
          surcharge: surcharge,
          status: "11",
        }
      });
      return Promise.reject(new Error("请先审核报价"))
    });
  }
}

class CloseConfirmChain {
  confirm ({ subjectId }, $vue) {
    $vue.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` });
    return Promise.reject(new Error("运单有关单待办需要处理，暂不能操作"))
  }
}

class ReconciliationChain {
  confirm ({ subjectId, waybillId, quotationId, surcharge, flag }, $vue) {

    return $vue.$confirm(`运单已对账，不能操作?`, "提示", {
      confirmButtonText: "我知道了",
      type: "warning",
      showClose: false,
      showCancelButton: false,
      closeOnClickModal: false
    }).then(() => {
      this.close({ waybillId, closeType: flag }, $vue);
      return Promise.reject(new Error("运单已结账，不能操作"))
    });
  }

  async close ({ waybillId, closeType }, $vue) {
    const url = `/transport/waybill/close/quote`;
    await axios.post(url, { waybillId });
    $vue.search();
    return Promise.reject(new Error("运单已对账，不能操作"))
  }
}

class CheckoutChain {
  confirm ({ subjectId, waybillId, quotationId, surcharge, flag }, $vue) {

    return $vue.$confirm(`运单已结账，不能操作?`, "提示", {
      confirmButtonText: "我知道了",
      type: "warning",
      showClose: false,
      showCancelButton: false,
      closeOnClickModal: false
    }).then(() => {
      this.close({ waybillId, closeType: flag }, $vue);
      return Promise.reject(new Error("运单已结账，不能操作"))
    });
  }

  async close ({ waybillId, closeType }, $vue) {
    const url = `/transport/waybill/close/quote`;
    await axios.post(url, { waybillId });
    $vue.search();
    return Promise.reject(new Error("运单已结账，不能操作"))
  }
}

class JoinReconciliationChain {
  confirm ({ subjectId }, $vue) {
    $vue.$message({ type: 'warning', message: `财务已将运单加入到对账单，不能操作，请联系财务处理` });
    return Promise.reject(new Error("财务已将运单加入到对账单，不能操作，请联系财务处理"))
  }
}

/**** flag ****/
// 1, "已向您发起了报价是否去审核"
// 2, "不能重复发起报价"
// 3, "运单关单待办"
// 4, "合单关单待办"
// 5, "已对账"
// 6, "已结账"
// 7, "已加入对账单"
const VALIDATOR_CHAINS = {
  0: new MatchConfirmChain(),
  1: new AuditConfirmChain(),
  2: new RepeatConfirmChain(),
  3: new CloseConfirmChain(),
  5: new ReconciliationChain(),
  6: new CheckoutChain(),
  7: new JoinReconciliationChain()
}


export default class QuotationValidator {
  async validate ({ subjectId, waybillId, surcharge, status }, $vue) {
    const checkUrl = `/quotation/check/${subjectId}`;
    const url = `/quotation/validate/${subjectId}`;
    // 所有状态的报价操作首先需要调度check接口校验
    let response = await axios.get(checkUrl);
    if (0 !== response.code) {
      $vue.$message({ type: 'error', message: response.message });
      return Promise.reject(new Error("报价验证失败"))
    }
    if (!response.data.validate) {
      const chainType = response.data.flag;
      const chain = VALIDATOR_CHAINS[chainType];
      if (chain) {
        await chain.confirm({ subjectId, waybillId, quotationId: response.data?.quotationId, surcharge, flag: response?.data?.flag }, $vue);
      }
    } else {
      // 待报价、草稿、被拒绝、补充报价需要调用validate接口再次校验
      if ([10, 0, 2, 3].includes(status)) {
        let { code, message, data } = await axios.get(url);
        if (0 !== code) {
          $vue.$message({ type: 'error', message });
          return Promise.reject(new Error("报价验证失败"))
        }

        if (!data.validate) {
          const chainType = data.flag;
          const chain = VALIDATOR_CHAINS[chainType];
          if (chain) {
            await chain.confirm({ subjectId, waybillId, quotationId: data.quotationId, surcharge }, $vue);
          }
        }
      }
    }

    // 返回true发起请求进行运单报价
    return true;
  }
}